#include "Observer.h"

void ConCreteObserverA::Update(const Subject* sub)
{
	string stat = sub->GetState();
	cout << "ObserverA notify " << stat << endl;
}

void ConCreteObserverB::Update(const Subject* sub)
{
	string stat = sub->GetState();
	cout << "ObserverB notify " << stat << endl;	
}

void ConCreteObserverC::Update(const Subject* sub)
{
	string stat = sub->GetState();
	cout << "ObserverC notify " << stat << endl;	
}



////////////////////////////////////////////////////////////////////////
void Subject::SetState(const string& state)
{
	_state = state;
}

string Subject::GetState() const
{
	return _state;
}
void Subject::Attach(IObserver* obs)
{	
	ObsIter iter = _obsers.begin();
	for(;iter != _obsers.end();iter++)
	{
		if( *iter == obs)
			return;
	}

	_obsers.push_back(obs);
}

void Subject::Detach(IObserver* obs)
{
	ObsIter iter = _obsers.begin();
	for(;iter != _obsers.end();iter++)
	{
		if( *iter == obs)
		{
			_obsers.erase(iter);
			break;
		}	
	}
}

void Subject::Notify()
{
	ObsIter iter = _obsers.begin();
	for(;iter != _obsers.end();iter++)
	{
		(*iter)->Update(this);
	}
}

int main()
{
	Subject sub;
	IObserver* obsA = new ConCreteObserverA;
	IObserver* obsB = new ConCreteObserverB;
	IObserver* obsC = new ConCreteObserverC;

	sub.Attach(obsA);
	sub.Attach(obsB);
	sub.Attach(obsC);


	sub.SetState("Event A Occurs");
	sub.Notify();

	sub.Detach(obsC);

	sub.Notify();

	delete obsA;
	delete obsB;
	delete obsC;
}
